package com.ghsc.ghxc.jsb.config

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatEditText
import com.cn.appcore.ext.dp
import com.ghsc.ghxc.jsb.R

class PasswordEditText @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : AppCompatEditText(context, attrs, defStyleAttr) {
    // 画笔
    private val mPaint: Paint by lazy { Paint() }

    // 一个密码所占的宽度
    private var mPasswordItemWidth = 0f

    // 密码的个数默认为6位数
    private val mPasswordNumber = 6

    // 密码圆点的颜色
    private var mPasswordColor = Color.parseColor("#333333")

    // 密码圆点的半径大小
    private var mPasswordRadius = 4

    //输入项间隔
    private var inputMargin = 15.dp.toFloat()

    //选中输入边框颜色
    private var selectInputStrokeColor = Color.parseColor("#E6E6E6")

    //未选中输入边框颜色
    private var notSelectInputStrokeColor = Color.parseColor("#E6E6E6")

    //输入框填充颜色
    private var inputSolidColor = Color.parseColor("#FFFFFF")

    //边框线宽度
    private var strokeLineWidth = 1.dp.toFloat()

    //输入框圆角半径
    private var inputCornerRadius = 2.dp.toFloat()

    /**
     * 初始化属性
     */
    private fun initAttributeSet(
        context: Context,
        attrs: AttributeSet?
    ) {
        val array =
            context.obtainStyledAttributes(attrs, R.styleable.PasswordEditText)

        mPasswordRadius = array.getDimension(
            R.styleable.PasswordEditText_passwordRadius,
            mPasswordRadius.dp.toFloat()
        ).toInt()
        mPasswordColor = array.getColor(
            R.styleable.PasswordEditText_passwordColor,
            mPasswordColor
        )
        selectInputStrokeColor = array.getColor(
            R.styleable.PasswordEditText_selectInputStrokeColor,
            selectInputStrokeColor
        )
        notSelectInputStrokeColor = array.getColor(
            R.styleable.PasswordEditText_notSelectInputStrokeColor,
            notSelectInputStrokeColor
        )
        inputSolidColor = array.getColor(
            R.styleable.PasswordEditText_inputSolidColor,
            inputSolidColor
        )
        inputMargin =
            array.getDimension(R.styleable.PasswordEditText_inputMargin, inputMargin)
        strokeLineWidth =
            array.getDimension(
                R.styleable.PasswordEditText_strokeLineWidth,
                strokeLineWidth
            )
        inputCornerRadius =
            array.getDimension(
                R.styleable.PasswordEditText_inputCornerRadius,
                inputCornerRadius
            )
        array.recycle()
    }

    /**
     * 初始化画笔
     */
    private fun initPaint() {
        mPaint.isAntiAlias = true
        mPaint.isDither = true
    }

    override fun onDraw(canvas: Canvas) {
        mPasswordItemWidth = (width - (mPasswordNumber - 1) * inputMargin) / mPasswordNumber
        // 绘制分割线
        drawInputBg(canvas)
        // 绘制密码
        drawHidePassword(canvas)
    }

    /**
     * 绘制输入框背景
     */
    private fun drawInputBg(canvas: Canvas) {
        mPaint.strokeWidth = strokeLineWidth

        var startWidth = 0f
        val textLength = text?.length ?: 0
        for (i in 0 until mPasswordNumber) {
            //画边框
            mPaint.style = Paint.Style.FILL
            mPaint.color = if (hasFocus()
                && (i == textLength || (i == mPasswordNumber - 1 && textLength == mPasswordNumber))
            ) {
                selectInputStrokeColor
            } else {
                notSelectInputStrokeColor
            }
            val strokeRecF =
                RectF(startWidth, 0f, startWidth + mPasswordItemWidth, height.toFloat())
//            canvas.drawRect(strokeRecF, mPaint)
            canvas.drawRoundRect(strokeRecF, inputCornerRadius, inputCornerRadius, mPaint)

            //画填充
            mPaint.color = inputSolidColor
            val solidRecF = RectF(
                startWidth + strokeLineWidth, strokeLineWidth,
                startWidth + mPasswordItemWidth - strokeLineWidth, height - strokeLineWidth
            )
//            canvas.drawRect(solidRecF, mPaint)
            canvas.drawRoundRect(solidRecF, inputCornerRadius, inputCornerRadius, mPaint)

            startWidth += (mPasswordItemWidth + inputMargin)
        }
    }

    /**
     * 绘制隐藏的密码
     */
    private fun drawHidePassword(canvas: Canvas) {
        val passwordLength = text?.length ?: 0
        mPaint.color = mPasswordColor
        // 设置画笔为实心
        mPaint.style = Paint.Style.FILL
        for (i in 0 until passwordLength) {
            val cx =
                i * (mPasswordItemWidth + inputMargin) + mPasswordItemWidth / 2
            canvas.drawCircle(
                cx, height / 2.toFloat(),
                mPasswordRadius.toFloat(),
                mPaint
            )
        }
    }

    init {
        initPaint()
        initAttributeSet(context, attrs)
//        // 设置输入模式是密码
//        inputType = EditorInfo.TYPE_NUMBER_VARIATION_PASSWORD
        // 不显示光标
        isCursorVisible = false
    }
}